package com.boc.langxiang.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

import com.boc.langxiang.R;
import com.boc.langxiang.entity.bean.ShareBean;
import com.boc.langxiang.entity.events.ShareEvent;
import com.boc.langxiang.util.T;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Li Guoxi on 2016/12/12.
 */

public class SharePadDlg extends Dialog {

    @BindView(R.id.share_recycler)
    RecyclerView shareRecycler;
    @BindView(R.id.close)
    ImageView close;

    private Context mContext;

    private ShareBean mShareBean;

    private BaseQuickAdapter<SHARE_MEDIA> mAdapter;

    private SHARE_MEDIA[] share_medias;

    public SharePadDlg(Context context, SHARE_MEDIA... share_medias) {
        super(context);
        this.mContext = context;
        this.share_medias = share_medias;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.setGravity(Gravity.CENTER);
        window.requestFeature(Window.FEATURE_NO_TITLE);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setContentView(R.layout.share_pad_dialog);
        ButterKnife.bind(this);
        initDialog();
    }

    private void initDialog() {
        shareRecycler.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        List<SHARE_MEDIA> share_mediaList = new ArrayList<>();
        for (int i = 0; i < share_medias.length; i++) {
            share_mediaList.add(share_medias[i]);
        }
        mAdapter = new BaseQuickAdapter<SHARE_MEDIA>(R.layout.share_pad_item, share_mediaList) {
            @Override
            protected void convert(BaseViewHolder helper, final SHARE_MEDIA item) {
                if (item.equals(SHARE_MEDIA.WEIXIN)) {
                    helper.setImageResource(R.id.share_icon, R.drawable.icon_share_weixin)
                            .setText(R.id.share_tv, "微信好友");
                } else if (item.equals(SHARE_MEDIA.WEIXIN_CIRCLE)) {
                    helper.setImageResource(R.id.share_icon, R.drawable.icon_share_weif)
                            .setText(R.id.share_tv, "朋友圈");
                } else if (item.equals(SHARE_MEDIA.QQ)) {
                    helper.setImageResource(R.id.share_icon, R.drawable.icon_share_qq)
                            .setText(R.id.share_tv, "QQ好友");
                } else if (item.equals(SHARE_MEDIA.SINA)) {
                    helper.setImageResource(R.id.share_icon, R.drawable.icon_share_sina)
                            .setText(R.id.share_tv, "新浪微博");
                }

                helper.getConvertView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mShareBean != null) {
                            new ShareAction((Activity) SharePadDlg.this.mContext)
                                    .setPlatform(item)
                                    .withMedia(mShareBean.getUmImage())
                                    .withTargetUrl(mShareBean.getTargetUrl())
                                    .withTitle(mShareBean.getTitle())
                                    .withText(mShareBean.getText())
                                    .setCallback(new UMShareListener() {
                                        @Override
                                        public void onResult(SHARE_MEDIA share_media) {
                                            T.showShort(mContext, "分享成功");
                                            EventBus.getDefault().post(new ShareEvent());
                                        }

                                        @Override
                                        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                                            T.showShort(mContext, "分享失败");
                                            Log.e("share_fail", throwable.getMessage());
                                        }

                                        @Override
                                        public void onCancel(SHARE_MEDIA share_media) {
                                            T.showLong(mContext, "分享已取消");
                                        }
                                    }).share();
                            dismiss();
                        }
                    }
                });
            }
        };
        shareRecycler.setAdapter(mAdapter);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public ShareBean getShareBean() {
        return mShareBean;
    }

    public SharePadDlg setShareBean(ShareBean shareBean) {
        this.mShareBean = shareBean;
        return this;
    }
}
